package com.erp.programmer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.erp.programmer.bean.CodeMsg;
import com.erp.programmer.dao.PurchaseOrderMapper;
import com.erp.programmer.dao.SellCompanyMapper;
import com.erp.programmer.domain.PurchaseOrder;
import com.erp.programmer.domain.PurchaseOrderDetail;
import com.erp.programmer.domain.SellCompany;
import com.erp.programmer.dto.*;
import com.erp.programmer.enums.PaymentStateEnum;
import com.erp.programmer.enums.StorageStateEnum;
import com.erp.programmer.service.IPurchaseOrderDetailService;
import com.erp.programmer.service.IPurchaseOrderService;
import com.erp.programmer.service.ProductService;
import com.erp.programmer.utils.CommonUtil;
import com.erp.programmer.utils.CopyUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 陈鑫
 * @since 2021-05-24
 */
@Service
@Transactional
public class PurchaseOrderServiceImpl extends ServiceImpl<PurchaseOrderMapper, PurchaseOrder> implements IPurchaseOrderService {
    @Autowired
    PurchaseOrderMapper purchaseOrderMapper;
    @Autowired
    IPurchaseOrderDetailService iPurchaseOrderDetailService;
    @Autowired
    ProductService productService;

    @Autowired
    private SellCompanyMapper sellCompanyMapper;

    /**
     * 添加订单
     */
    @Override
    public ResponseDto<PageDto<PurchaseOrderDto>> insertPurchaseOrder(PurchaseOrderDto purchaseOrderDto) {
        // 采购单ID
        String purchaseId = String.valueOf(IdWorker.getId());

        BigDecimal totalPrice = new BigDecimal(0);
        for (PurchaseOrderDetailDto purchaseOrderDetailDto:purchaseOrderDto.getPurchaseOrderDetailDtoList()) {
            BigDecimal bigDecimal = new BigDecimal(purchaseOrderDetailDto.getNumber());
            BigDecimal price = productService.getById(purchaseOrderDetailDto.getProductId()).getPrice().multiply(bigDecimal);
            purchaseOrderDetailDto.setPurchaseId(purchaseId);
            totalPrice=totalPrice.add(price);
            purchaseOrderDetailDto.setTotalPrice(price);
            if(!iPurchaseOrderDetailService.save(CopyUtil.copy(purchaseOrderDetailDto,PurchaseOrderDetail.class))){
                return ResponseDto.errorByMsg(CodeMsg.SUBMIT_ORDER_ERROR);
            }
        }
        purchaseOrderDto.setId(purchaseId);
        purchaseOrderDto.setTotalPrice(totalPrice);
        purchaseOrderDto.setPaymentState(PaymentStateEnum.UNPAID.getCode());
        purchaseOrderDto.setStorageState(StorageStateEnum.NO_STORED.getCode());
        PurchaseOrder purchaseOrder = CopyUtil.copy(purchaseOrderDto, PurchaseOrder.class);
        if(purchaseOrderMapper.insert(purchaseOrder)<1) {
            return ResponseDto.errorByMsg(CodeMsg.SUBMIT_ORDER_ERROR);
        }
        List<PurchaseOrderDto> purchaseOrderDtoList = CopyUtil.copyList(list(), PurchaseOrderDto.class);
        ResponseDto<PageDto<PurchaseOrderDto>> purchaseOrder1 = getPurchaseOrder(new PageDto<PurchaseOrderDto>());
        purchaseOrder1.setMsg("添加成功");
        return purchaseOrder1;
    }

    /**
     * 根据条件获取订单信息，无条件返回全部
     * @param pageDto
     * @return
     */
    @Override
    public ResponseDto<PageDto<PurchaseOrderDto>> getPurchaseOrder(PageDto<PurchaseOrderDto> pageDto) {
        QueryWrapper<PurchaseOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("purchase_date");
        if ("id".equals(pageDto.getSearchType()) && !CommonUtil.isEmpty(pageDto.getSearchContent())) {
            queryWrapper.eq("id",pageDto.getSearchContent());
        }
        if (pageDto.getPage() ==null) {
            pageDto.setPage(1);
        }
        pageDto.setSize(5);
        PageHelper.startPage(pageDto.getPage(), pageDto.getSize());
        List<PurchaseOrder> purchaseOrderList = purchaseOrderMapper.selectList(queryWrapper);
        List<PurchaseOrderDto> purchaseOrderDtoList = CopyUtil.copyList(purchaseOrderList, PurchaseOrderDto.class);
        purchaseOrderDtoList.forEach(e->{
            SellCompany sellCompany = sellCompanyMapper.selectById(e.getSellerId());
            SellCompanyDto sellCompanyDto = CopyUtil.copy(sellCompany, SellCompanyDto.class);
            e.setSellCompanyDto(sellCompanyDto);
        });
        PageInfo<PurchaseOrder> pageInfo = new PageInfo<>(purchaseOrderList);
        pageDto.setTotal(pageInfo.getTotal());
        pageDto.setList(purchaseOrderDtoList);
        return ResponseDto.success(pageDto);
    }

    /**
     * 修改入库状态
     * @param id
     * @param storageState
     * @return
     */
    @Override
    public Boolean updateStorageState(String id,Integer storageState) {
        PurchaseOrder purchaseOrder = purchaseOrderMapper.selectById(id);
        if (purchaseOrder==null) {
            return false;
        }
        purchaseOrder.setStorageState(storageState);
        if (purchaseOrderMapper.updateById(purchaseOrder)<1) {
            return false;
        }
        return true;
    }

    /**
     * 修改付款状态
     * @param id
     * @param paymentState
     * @return
     */
    @Override
    public ResponseDto<PageDto<PurchaseOrderDto>> updatePaymentState(String id, Integer paymentState) {
        PurchaseOrder purchaseOrder = purchaseOrderMapper.selectById(id);
        if (purchaseOrder==null) {
            return ResponseDto.errorByMsg(CodeMsg.DATA_ERROR);
        }
        purchaseOrder.setPaymentState(paymentState);
        purchaseOrderMapper.updateById(purchaseOrder);
        return getPurchaseOrder(new PageDto<>());
    }
}
